import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        arr: [],
        detail: {},
        list: [
            {
              id: "1",
              name: "张三",
              tel: "13000000000",
              address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
              isDefault: true,
            },
            {
              id: "2",
              name: "李四",
              tel: "1310000000",
              address: "浙江省杭州市拱墅区莫干山路 50 号",
            },
          ],
    },
    mutations: {
        setArr(state, payload) {
            state.arr = payload
        },
        setDetail(state, payload) {
            state.arr.forEach((item, index) => {
                if (item.id == payload) {
                    state.detail = item
                }
            })
        },
        // 新增地址
        addressArrPush(state, payload){
            state.list.push(payload)
            console.log(state.list)
        },
        // 编辑地址
        editressArrPush(state, payload){
            state.list[payload.ind] = payload
        },
        // 地址删除
        shanchuAddress(state, payload){
            state.list.splice(payload,1)
        },
        // 加入购物车的方法
        addShop1(state, payload) {
            state.arr = state.arr.map((item, index) => {
                if (payload == item.id) {
                    item.num++
                }
                return item
            })
        },
        // 点击商品加加
        addGoods(state, payload) {
            state.arr = state.arr.map((item, index) => {
                if (payload == item.id) {
                    item.num++
                }
                return item
            })
        },
        // 点击商品减减
        delGoods(state, payload) {
            state.arr = state.arr.map((item, index) => {
                if (payload == item.id) {
                    item.num--
                }
                return item
            })
        },
        // 商品的选中与不选中
        check(state, payload) {
            state.arr = state.arr.map((item, index) => {
                if (payload == item.id) {
                    item.checked = !item.checked
                }
                return item
            })
        },
        // 全选
        quanxuan(state, payload) {
            console.log(payload)
            //     state.arr = state.arr.map((item,index)=>{
            //         return  item.checked = payload
            //    })
            state.arr.map((item, index) => {
                return item.checked = payload
            })
            console.log(state.arr)
            // state.arr =  state.arr.map((item,index)=>{
            //          item.checked = payload
            //          return item
            //      })
        },
        delAddress(state,payload){
            state.list.splice(payload,1)
        }
    },
    actions: {
        getList(vuex, payload) {
            // 让arr没有的时候在去发请求，否则不发请求，不发请求状态就被保存下来了
            if (vuex.state.arr.length <= 0) {
                axios.get("/list").then(res => {
                    vuex.commit('setArr', res.data)
                })
            }

        }
    },
    modules: {}
})